<?php

namespace app\common\model;

use think\Model;
use think\Db;

class PayBill extends Model
{
    protected $insert = ['ctime'];
    
    protected function setCtimeAttr()
    {
        return time();
    }
    
    public function room()
    {
        return $this->hasOne('Room', 'bn', 'room_bn')->field('id, bn, name, price, mktprice, image_id');
    }
    
    public function user()
    {
        return $this->hasOne('User', 'id', 'user_id')->field(['id', 'avatar', 'nickname', 'password', 'mobile']);
    }
    /**
     * 买单订单支付
     * @param type $room_bn     房间号
     * @param type $amount      金额
     * @param type $user_id     用户ID
     * @param array $order_ids  单点订单ID 废弃
     * @param array $taocan_ids 套餐订单Id 废弃
     * @return array
     * @throws \Exception
     */
    public function pay($room_bn, $amount, $user_id, $order_ids = [], $taocan_ids = [])
    {
        $hour=date("G");
        if($hour < 3){
            $begintime=strtotime(date("Y-m-d",strtotime("-1 day"))." 0:0:0");
            $endtime=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");
        }else{
            $begintime=strtotime(date("Y-m-d",time())." 0:0:0");
            $endtime=strtotime(date("Y-m-d",time())." 23:59:59");
        }
        
        Db::startTrans();
        try {
            $pay_bill_model = $this;
            $pay_bill_model->bill_bn = build_order_no();
            $pay_bill_model->room_bn = $room_bn;
            $pay_bill_model->user_id = $user_id;
            $pay_bill_model->amount = $amount;
            $pay_bill_model->pay_status = 1;
            if(!$pay_bill_model->save()) {
                throw new \Exception('订单生成失败');
            }
            //余额变更
            $balance_model = new Balance();
            $res = $balance_model->change($user_id, 1, $amount, $pay_bill_model->id);
            if(!$res['status']){
                throw new \Exception($res['msg']);
            }
            //订单预支付更新
            $order_model = new Order();
            $condition = [
                ['after_status', '=', '0'],
                ['pay_status', '=', '2'],
                ['ctime', ['EGT',$begintime],['ELT',$endtime],'and'],
                ['status', 'neq', '3'],
                ['room_bn', '=', $room_bn]
            ];
            $order_ids = $order_model->where($condition)->column('order_id');
            if(!empty($order_ids)) {
                $res = $order_model->where('order_id', 'in', $order_ids)->setField('after_status', '1');
                if(!$res) {
                    throw new \Exception('单点订单异常');
                }
            }
            //套餐订单
            $taocan_order = new TaocanOrder();
            $taocan_condition = [
                ['after_status', '=', '0'],
                ['pay_status', '=', '1'],
                ['ctime', ['EGT',$begintime],['ELT',$endtime],'and'],
                ['room_bn', '=', $room_bn]
             ];
            $taocan_ids = $taocan_order->where($taocan_condition)->column('id');
            if (!empty($taocan_ids)) {
               $res = $taocan_order->where('id', 'in', $taocan_ids)->setField('after_status', '1');
               if(!$res) {
                   throw new \Exception('套餐订单异常');
               }
            }
            Db::commit();
        } catch (\Exception $ex) {
            Db::rollback();
            return [
                'status'    =>  false,
                'msg'       =>  $ex->getMessage(),
                'data'      =>  []
            ];
        }
        //后台语音播报
        doRequest(config('jshop.jpush_host'), ['id' => $pay_bill_model->id, 'method' => 'pay_bill']);
        return [
            'status'    =>  true,
            'msg'       =>  '订单支付成功',
            'data'      =>  $pay_bill_model->id
        ];
    }
}
